//
//  LXMoreViewController.m
//  bgms_d
//
//  Created by Mountain on 4/3/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXMoreViewController.h"
#import "LXAboutViewController.h"
#import "LXDevSettingViewController.h"
#import "LXAlertTableViewController.h"
#import "LXHomeViewController.h"

@interface LXMoreViewController () <UITableViewDelegate, UITableViewDataSource, LXActionSheetDelegate>

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *sheetsArray;

@end

@implementation LXMoreViewController

#pragma mark -  public method

#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];
    
    [self loadUI];
    
    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated
{

}

#pragma mark -  over write

#pragma mark -  setter & getter

#pragma mark -  private method
- (void)initializationData
{
    self.dataArray =
    @[@[@{@"name" : NSLocalizedString(@"Thermometer", nil), @"image" : @"moreIcon1"}],
      @[@{@"name" : NSLocalizedString(@"TriggeredAlarms", nil), @"image" : @"moreIcon2"}],
      @[@{@"name" : NSLocalizedString(@"Language", nil), @"image" : @"moreIcon3"},
        @{@"name" : NSLocalizedString(@"About", nil), @"image" : @"moreIcon4"},
        @{@"name" : NSLocalizedString(@"FeedbackSupport", nil), @"image" : @"moreIcon5"}]];
    
    self.sheetsArray = @[NSLocalizedString(@"Auto", nil), NSLocalizedString(@"English", nil), NSLocalizedString(@"Deutsch", nil), NSLocalizedString(@"Franch", nil), NSLocalizedString(@"Espanch", nil), NSLocalizedString(@"Italiano", nil), NSLocalizedString(@"Nederlands", nil)];
}

- (void)loadUI
{
    self.view.backgroundColor = kAppBackgroundColor;
    self.tableView.backgroundColor = kAppBackgroundColor;
    [self buildNavi];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{
    
}

#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle
- (void)deleteDown:(UIButton *)sender
{
    
}

- (void)switchDown:(UISwitch *)sender
{
    
}
#pragma mark -  delegate
#pragma mark
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, [LXAutoLength lx_L:10])];
    footView.backgroundColor = kAppBackgroundColor;
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return [LXAutoLength lx_L:10];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LXAutoLength lx_L:52];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [self.dataArray objectAtIndex:section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arrays = [self.dataArray objectAtIndex:indexPath.section];
    NSDictionary *cellDic = [arrays objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:cellDic[@"image"]];
    cell.textLabel.text = cellDic[@"name"];
    cell.textLabel.textColor = kAppNormalTextColor;
    UIView *nextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    showLabel.font = [UIFont lx_systemFontOfSize:16];
    showLabel.textColor = kAppGrayBgColor;
    showLabel.textAlignment = NSTextAlignmentRight;
    showLabel.text = @"";
    showLabel.tag = 1;
    UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake(85, 12, 10, 16)];
    showImage.image = [UIImage imageNamed:@"Chevron"];
    nextView.tag = 2;
    [nextView addSubview:showLabel];
    [nextView addSubview:showImage];
    cell.accessoryView = nextView;
    if (indexPath.row != arrays.count - 1) {
        [cell drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(-0.5, 0, 0, 0)];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        LXDevSettingViewController *vc = [[LXDevSettingViewController alloc] initWithNibName:NSStringFromClass([LXDevSettingViewController class]) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 1) {
        LXAlertTableViewController *vc = [[LXAlertTableViewController alloc] initWithNibName:NSStringFromClass([LXAlertTableViewController class]) bundle:nil];
        vc.tempArray = self.tempArray;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            LXActionSheet *sheet = [[LXActionSheet alloc] initWithTitle:NSLocalizedString(@"SelectLanguage", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:@"" otherButtonTitles:self.sheetsArray];
            [sheet showInView:self.view];
        } else if (indexPath.row == 1) {
            LXAboutViewController *vc = [[LXAboutViewController alloc] initWithNibName:NSStringFromClass([LXAboutViewController class]) bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark - LXSwitchViewDelegate
- (void)LXActionSheet:(LXActionSheet *)actionSheet didSelectIndex:(NSInteger)buttonIndex
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIView *view = [cell.accessoryView viewWithTag:2];
    UILabel *showLabel = [view viewWithTag:1];
    showLabel.text = [self.sheetsArray objectAtIndex:buttonIndex - 1];
    
    [LanguageManager saveLanguageByIndex:buttonIndex - 1];
    [self reloadRootViewController];
}

- (void)reloadRootViewController
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    LXHomeViewController *homeVC = [[LXHomeViewController alloc] initWithNibName:@"LXHomeViewController" bundle:nil];
    LXThemeColorNavigationController *themeNav = [[LXThemeColorNavigationController alloc] initWithRootViewController:homeVC];
    delegate.window.rootViewController = themeNav;
}
#pragma mark -  lazy load

#pragma mark -  memoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
